package com.anrongtec.zcpt.fragment.myfocusfragment;


import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.anrongtec.zcpt.R;
import com.anrongtec.zcpt.adapter.NewsListViewAdapter;
import com.anrongtec.zcpt.model.focus.FocusPeopleInfo;
import com.anrongtec.zcpt.ui.activity.myfocus.FocusAdapter;
import com.anrongtec.zcpt.ui.activity.myfocus.FocusPeopleDetailActivity;
import com.anrongtec.zcpt.ui.activity.myfocus.FocusPeopleDetailInfoActivity;
import com.anrongtec.zcpt.utils.ToastShow;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import static com.anrongtec.zcpt.R.id.lv_focus_people_info;

/**
 * A simple {@link Fragment} subclass.
 *
 * @author liuhui
 */
public class FocusPeopleFragment extends Fragment {
    private TagFlowLayout tf_tag_fragment1;
    private ArrayList<String> tagTitleData1;
    private FocusAdapter adapter;

    //    private PullToRefreshListView pullToRefreshListView;
    private ListView focusList;
    public List<FocusPeopleInfo> newsDataList;
    private NewsListViewAdapter newsListViewAdapter;

    private int[] images = {R.drawable.people1, R.drawable.people2, R.drawable.people3};
    private int MY_PERMISSIONS_REQUEST_CALL_PHONE = 10;

    public FocusPeopleFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_focus_people, container, false);
        tf_tag_fragment1 = (TagFlowLayout) view.findViewById(R.id.tf_tag_fragment_focus);
        focusList = (ListView) view.findViewById(lv_focus_people_info);
        initData();
        initEvent();
        testLoadNewsData();

        return view;
    }

    private void initEvent() {
        newsDataList = new ArrayList<>();
        FocusPeopleInfo focusInfo1 = new FocusPeopleInfo("张三", "男", "汉族","2017/7/7", "110196928371293", "北京市海淀区西三旗育新家园",
                "涉访", "天津", "北京");
        FocusPeopleInfo focusInfo2 = new FocusPeopleInfo("张婷",  "女", "汉族", "2017/7/6","110047329847239", "北京市海淀区西三旗育新家园",
                "涉毒", "北京", "河北");
        FocusPeopleInfo focusInfo3 = new FocusPeopleInfo("王国华",  "男", "汉族", "2017/7/7","11034856356385", "北京市海淀区西三旗育新家园",
                "涉访", "北京", "南京");
        newsDataList.add(focusInfo1);
        newsDataList.add(focusInfo2);
        newsDataList.add(focusInfo3);

        CommonAdapter<FocusPeopleInfo> adapter = new CommonAdapter<FocusPeopleInfo>(getActivity(),
                R.layout.item_focus_people_info, newsDataList) {
            @Override
            protected void convert(ViewHolder holder, FocusPeopleInfo focusPeopleInfo, final int position) {
                holder.setText(R.id.tv_focus_people_name, focusPeopleInfo.getName());
                holder.setText(R.id.tv_focus_people_time, focusPeopleInfo.getOnTime());
                holder.setText(R.id.tv_focus_people_sex, focusPeopleInfo.getSex());
                holder.setText(R.id.tv_focus_people_nation, focusPeopleInfo.getNation());
                holder.setText(R.id.tv_focus_people_idNum, focusPeopleInfo.getIdNum());
                holder.setText(R.id.tv_focus_people_location, focusPeopleInfo.getLocation());
                holder.setText(R.id.tv_focus_people_crimeType, focusPeopleInfo.getCrimeType());
                holder.setText(R.id.tv_focus_people_dynamicInfo_from, focusPeopleInfo.getFrom());
                holder.setText(R.id.tv_focus_people_dynamicInfo_to, focusPeopleInfo.getTo());
                holder.setImageResource(R.id.iv_focus_people_photo, images[position]);
                holder.setOnClickListener(R.id.ll_focus_people_attention, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ToastShow.shortMessage(getActivity(), "关注成功");
                    }
                });
                holder.setOnClickListener(R.id.ll_focus_people_tel, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        checkPermission();
                    }
                });
                holder.setOnClickListener(R.id.tv_focus_people_name, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(getActivity(),
                                FocusPeopleDetailActivity.class);
                        intent.putExtra("positionData", newsDataList.get(position));
                        getActivity().startActivity(intent);
                    }
                });
            }
        };
        focusList.setAdapter(adapter);
        focusList
                .setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
                        Intent intent = new Intent(getActivity(),
                                FocusPeopleDetailInfoActivity.class);
                        intent.putExtra("positionData", newsDataList.get(position));
                        getActivity().startActivity(intent);
                    }
                });
    }

    private void checkPermission() {
        if (ContextCompat.checkSelfPermission(getActivity(),
                Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(getActivity(),
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    MY_PERMISSIONS_REQUEST_CALL_PHONE);
        } else {
            Uri uri = Uri.parse("tel:0800000123");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    }

    private void initData() {
        tagTitleData1 = new ArrayList<>();
        tagTitleData1.add("张三 ");
        tagTitleData1.add("张婷");
        tagTitleData1.add("王国华");
        tagTitleData1.add("王五");
        tagTitleData1.add("冯云");

        adapter = new FocusAdapter(tagTitleData1, getActivity());
        tf_tag_fragment1.setAdapter(adapter);
        tf_tag_fragment1.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                TextView textView = (TextView) view.findViewById(R.id.tv_focus_people);
                textView.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
                return true;
            }
        });
    }

    /**
     * 测试读取新闻列表项中的数据
     */
    private void testLoadNewsData() {
        Log.w("当前newsDataList中新闻数量为", String.valueOf(newsDataList.size()));
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == MY_PERMISSIONS_REQUEST_CALL_PHONE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission Granted
                Uri uri = Uri.parse("tel:13671116291");
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            } else {
                ToastShow.shortMessage(getActivity(), "权限拒绝，请重新启动授权");
            }
        }


    }
}
